package com.liugui.iostream;

import java.io.File;
import java.io.IOException;

/**
 * @ClassName: FileDemo
 * @Author: liugui
 * @Date: 2020-11-05 15:08
 **/
public class FileDemo {
    public static void main(String[] args) {
        File f1 =new File("E:\\home\\project\\1.txt");
        //File(String parent,String child)
        File f2 =new File("E:\\home\\project","2.txt");
        //separator 跨平台分隔符
        File f3 =new File("E:\\home"+File.separator+"project");
        File f4 =new File(f3,"3.txt");


        try {
            System.out.println(f1);
            if (!f3.exists()){
                f3.mkdir();//创建文件夹
            }
            //当文件存在时返回 false；不存在时返回 true
            System.out.println(f2.createNewFile());
            //当文件不存在时返回 false
            System.out.println(f3.delete());


            //列出磁盘下的文件和文件夹
            File[] files =File.listRoots();
            File file = files[files.length-2];//倒数第二个盘 前提是length>2
            System.out.println(file);
            if(file.length()>0){
                String[] filenames =file.list();
                for(String filename: filenames){
                    System.out.println(filename);
                }
            }

        }catch(IOException e) {
            e.printStackTrace();
        }
    }
}
